<?php

namespace Biz\model\brandActivity;

use Doraemon\pockets\datebase\BaseDbModel;

class BlindBoxActivityPrize extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_brand_activity';

    const _DATABASE_ = 'brand_activity';

    const _TABLE_NAME = 'blind_box_activity_prize';

    const ACTIVITY_TYPE_BLIND_BOX = 1;//活动类型，超级盲盒

    const PRIZE_STATE_NORMAL = 0;//默认状态
    const PRIZE_STATE_RECEIVED = 1;//已经领取（已中奖）注意，不能删除
    const PRIZE_STATE_DEL = 9;//已删除

    const PRIZE_TYPE_GOODS = BlindBoxActivity::PRIZE_TYPE_GOODS;//实物奖品
    const PRIZE_TYPE_SHOP_COUPON = BlindBoxActivity::PRIZE_TYPE_SHOP_COUPON;//全店通用券
    const PRIZE_TYPE_GOOD_COUPON = BlindBoxActivity::PRIZE_TYPE_GOOD_COUPON;//单品优惠券
    const PRIZE_TYPE_JD_CARD = BlindBoxActivity::PRIZE_TYPE_JD_CARD;//京东卡
    const PRIZE_TYPE_LIMIT_CARD = BlindBoxActivity::PRIZE_TYPE_LIMIT_CARD;//限定卡
    const PRIZE_TYPE_POINT = BlindBoxActivity::PRIZE_TYPE_POINT;//积分
    const PRIZE_TYPE_CUE_CARD = BlindBoxActivity::PRIZE_TYPE_CUE_CARD;//提示卡
    const PRIZE_TYPE_EMPTY_BOX = BlindBoxActivity::PRIZE_TYPE_EMPTY_BOX;//空盒子

    /**
     * 过滤盲盒中奖类型
     */
    public const FILTER_LOTTERY_TYPE = [
        BlindBoxActivity::PRIZE_TYPE_GOODS,//实物奖品
        BlindBoxActivity::PRIZE_TYPE_SHOP_COUPON,//全店通用券
        BlindBoxActivity::PRIZE_TYPE_GOOD_COUPON,//单品优惠券
        BlindBoxActivity::PRIZE_TYPE_JD_CARD,//京东卡
    ];


    public static function getActivityPrizeList($activityIds)
    {
        $result = [];
        if ($activityIds) {
            $where = [
                'activity_id' => $activityIds,
                'prize_state[!]' => self::STATUS_DELETE
            ];
            $list = self::selectByWhere($where);
            foreach ($list as $row) {
                $result[$row['activity_id']][] = $row;
            }
        }
        return $result;
    }

    public static function getByWhere($where, $select = '*')
    {
        return self::get($select, $where);
    }

    public static function getInfoByIdOfMaster($id)
    {
        $info = self::select_master("*", ['id' => $id]);
        if ($info) {
            $info = array_shift($info);
        }
        return $info;
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);
        return $count;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}